package cz.muni.fi.pa165.init;

import cz.muni.fi.pa165.entity.Customer;
import cz.muni.fi.pa165.entity.Delivery;
import cz.muni.fi.pa165.entity.Employee;
import cz.muni.fi.pa165.entity.Package;
import cz.muni.fi.pa165.enums.AdminRole;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Class initialize some sample data in database.
 * 
 * @author <a href="mailto:vramik at redhat.com">Vlastislav Ramik</a>
 */
@Singleton
@Startup
public class Initializer {

    @PersistenceContext
    private EntityManager em;
    
    private Customer c1;
    private Customer c2;
    private Employee e1;
    private Employee e2;
    private Package p1;
    private Package p2;
    private Package p3;
    private Package p4;
    private Package p5;
    
    @PostConstruct
    public void initialize() {
        e1 = persistEmployee("e1", 1977, 11, 15,"pass",AdminRole.Manager);
        e2 = persistEmployee("e2", 2000, 1, 4,"pass",AdminRole.Manager);
        persistEmployee("e3", 1988, 2, 18,"pass",AdminRole.DeliveryBoy);
        persistEmployee("rest", 1988, 2, 18,"rest",AdminRole.Manager);
        
        c1 = persistCustomer("123456789/0100", "Botanicka 68a", "Brno", "Czech Republic", "FI", "60200");
        c2 = persistCustomer("782387623/0100", "Hrncirska 5", "Brno", "Czech Republic", "firma", "62100");
        persistCustomer("008325383/0100", "Purkynova 99", "Brno", "Czech Republic", "Red Hat", "61200");
        
        p1 = persistPackage(10, 10, 100, new BigDecimal(123));
        p2 = persistPackage(20, 240, 140, new BigDecimal(1999));
        p3 = persistPackage(122, 13, 19, new BigDecimal(4));
        p4 = persistPackage(1, 134, 19, new BigDecimal(43));
        p5 = persistPackage(122, 313, 159, new BigDecimal(64));
        
        persistDelivery(new Date(), c1, e1, null, "K Babe 6", "Brno", "Jan Novak", "62100", p1, p2);
        persistDelivery(new Date(new Date().getTime() - 134325L), c1, e2, new Date(), "Hlavni 1368", "Ostrava", "Anna Koci", "73800", p3);
        persistDelivery(new Date(), c2, e2, new Date(), "Hlavni 1368", "Ostrava", "Josef Prikryl", "73800", p4, p5);
    
    }

    private Customer persistCustomer(String accountNumber, String address, String city, String country, String name, String zip) {
        Customer result = new Customer();
        result.setAccountNumber(accountNumber);
        result.setAddress(address);
        result.setCity(city);
        result.setCountry(country);
        result.setName(name);
        result.setZip(zip);
        em.persist(result);
        return result;
    }

    private Employee persistEmployee(String name, int year, int month, int date, String pass, AdminRole role) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, date);
        
        Employee result = new Employee();
        result.setBirthDate(cal.getTime());
        result.setName(name);
        result.setRole(role);
        result.setPassword(pass);
        em.persist(result);
        return result;
    }

    private Package persistPackage(int height, int width, int weight, BigDecimal price) {
        Package result = new Package();
        result.setHeight(height);
        result.setWidth(width);
        result.setWeight(weight);
        result.setPrice(price);
        em.persist(result);
        return result;
    }

    private void persistDelivery(Date creationDate, Customer customer, Employee deliveryBoy, Date deliveryDate, 
            String recipientAddress, String recipientCity, String recipientName, String recipientZip, Package ... packages) {
        Delivery result = new Delivery();
        result.setCreationDate(creationDate);
        result.setCustomer(customer);
        result.setDeliveryBoy(deliveryBoy);
        result.setDeliveryDate(deliveryDate);
        result.setPackages(Arrays.asList(packages));
        result.setRecipientAddress(recipientAddress);
        result.setRecipientCity(recipientCity);
        result.setRecipientName(recipientName);
        result.setRecipientZip(recipientZip);
        em.persist(result);
    }
}
